package com.cory.module.oplog.core.handler;

import com.cory.boot.utils.CommonData;
import com.cory.boot.utils.SpringUtils;
import com.cory.module.oplog.core.annotation.OpLogAttr;
import com.cory.module.oplog.core.model.AttributeModel;
import com.cory.module.oplog.core.richText.Html2Text;
import com.cory.module.oplog.core.richText.RichTextHandler;
import com.cory.module.oplog.core.wrapper.FieldWrapper;
import com.cory.module.oplog.mapper.OplogMapper;
import com.cory.utils.lang.StringUtils;

public enum BuiltinTypeHandler {
    NORMAL {   // 普通类型
        @Override
        public AttributeModel handlerAttributeChange(FieldWrapper fieldWrapper) {
            AttributeModel baseAttributeModel = new AttributeModel();
            baseAttributeModel.setOldValue(fieldWrapper.getOldValueString());
            baseAttributeModel.setNewValue(fieldWrapper.getNewValueString());
            return baseAttributeModel;
        }
    },
    IMAGE {   // 图片类型
        @Override
        public AttributeModel handlerAttributeChange(FieldWrapper fieldWrapper) {
            return NORMAL.handlerAttributeChange(fieldWrapper);
        }
    },
    DICT {   // 字段，对应字典表的值
        @Override
        public AttributeModel handlerAttributeChange(FieldWrapper fieldWrapper) {
            AttributeModel baseAttributeModel = new AttributeModel();
            
            OpLogAttr logTag = fieldWrapper.getLogTag();
            if (StringUtils.isNotBlank(fieldWrapper.getOldValueString())) {
                baseAttributeModel.setOldValue(CommonData.getDictValue(logTag.dictCode(), fieldWrapper.getOldValueString()));
            }
            if (StringUtils.isNotBlank(fieldWrapper.getNewValueString())) {
                baseAttributeModel.setNewValue(CommonData.getDictValue(logTag.dictCode(), fieldWrapper.getNewValueString()));
            }
            return baseAttributeModel;
        }
    },
    FOREIGN {   // 外键，对应其他表的列
        @Override
        public AttributeModel handlerAttributeChange(FieldWrapper fieldWrapper) {
            AttributeModel baseAttributeModel = new AttributeModel();
            
            OpLogAttr logTag = fieldWrapper.getLogTag();
            OplogMapper mapper = SpringUtils.getBean(OplogMapper.class);
            if (StringUtils.isNotBlank(fieldWrapper.getOldValueString())) {
                baseAttributeModel.setOldValue(mapper.getObjectData(logTag.foreignTable(), logTag.foreignTableKey(), fieldWrapper.getOldValueString(), logTag.foreignTableData()));
            }
            if (StringUtils.isNotBlank(fieldWrapper.getNewValueString())) {
                baseAttributeModel.setNewValue(mapper.getObjectData(logTag.foreignTable(), logTag.foreignTableKey(), fieldWrapper.getNewValueString(), logTag.foreignTableData()));
            }
            return baseAttributeModel;
        }
    },
    RICHTEXT { // 富文本类型
        @Override
        public AttributeModel handlerAttributeChange(FieldWrapper fieldWrapper) {
            String simpleOldValue = Html2Text.simpleHtml(fieldWrapper.getOldValueString());
            String simpleNewValue = Html2Text.simpleHtml(fieldWrapper.getNewValueString());
            // Delete the format and leave the main content behind.
            if (simpleOldValue == null || simpleNewValue == null || simpleOldValue.equals(simpleNewValue)) {
                // The main content is the same, the same
                return null;
            } else {
                AttributeModel baseAttributeModel = new AttributeModel();
                baseAttributeModel.setOldValue(fieldWrapper.getOldValueString());
                baseAttributeModel.setNewValue(fieldWrapper.getNewValueString());
                baseAttributeModel.setDiffValue(RichTextHandler.diffText(fieldWrapper.getOldValueString(), fieldWrapper.getNewValueString()));
                return baseAttributeModel;
            }
        }
    }
    
    
    ;

    public abstract AttributeModel handlerAttributeChange(FieldWrapper fieldWrapper);
}
